package net.huashitong.supervision.task.service;

import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.service.SysLoginService;
import com.sirdc.modules.sys.service.SysUserService;
import com.sirdc.modules.utils.ObjectUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/7/22
 * Time: 20:14
 * To change this template use File | Settings | File Templates.
 * Description:定时调度任务,自动计算任务是否逾期
 */

@Service
public class OverdueScheduleService {
    @Lazy
    @Autowired
    private TaskMainService taskMainService;
    @Autowired
    private TaskUnitService taskUnitService;
    @Autowired
    private TaskSubService taskSubService;
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private TaskUserService taskUserService;
    @Autowired
    private SysUserService userService;
    @Autowired
    private SMSService smsSerivce;
    @Autowired
    private SMSUtilService smsUtilService;
    @Autowired
    private SpecialTaskService specialTaskService;
    //定时查询任务是否逾期
    public void overdueTask() throws ParseException {
        Date day = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //取当前日期
        String today = format.format(day);
        //查询所有任务
        List<TaskUnit> unitList = taskUnitService.query();
        List<SpecialTask> specialTasks = specialTaskService.query();//所有特殊任务
        for (SpecialTask specialTask : specialTasks) {
            if (!"4".equals(specialTask.getType())&&!"2".equals(specialTask.getType())){
                //判断逾期
                Date parse = format.parse(specialTask.getRoundLimit());

                long d = format.parse(today).getTime() - parse.getTime();

                Double ceil= Math.ceil(d / (3600 * 24 * 1000));
//                String mobile = userService.getById(taskUnit.getUserId()).getMobile();
                //如果今天大于任务期限时间,任务就逾期
                if (ceil > 0) {
                    String s = Double.toString(ceil);
                    specialTask.setOverdueDay(s);
                    specialTaskService.update(specialTask);
                }else if (ceil == 0) {
                    SMSEntity smsEntity = new SMSEntity();
                    smsEntity.setSpecialId(specialTask.getSysId());
                    smsEntity.setType("2");//逾期短信
                    smsEntity.setState("0");//未发送
                    smsSerivce.save(smsEntity);
                }else if (ceil == -1) {
                    SMSEntity smsEntity = new SMSEntity();
                    smsEntity.setSpecialId(specialTask.getSysId());
                    smsEntity.setType("1");//提前短信
                    smsEntity.setState("0");//未发送
                    smsSerivce.save(smsEntity);
                }
            }
        }
        for (TaskUnit taskUnit : unitList) {
            String createUser = taskUnit.getCreateUser();
            SysUser taskCreateUser = userService.getById(createUser);
            //判断当前任务是否完成
            //如果当前任务状态不是已完成或未完成
            if (!"4".equals(taskUnit.getType()) && !"2".equals(taskUnit.getType())) {
                //判断任务是否逾期
                //将任务时间字符串转为时间格式
                Date parse = format.parse(taskUnit.getTimeLimit());

                long d = format.parse(today).getTime() - parse.getTime();

                Double ceil= Math.ceil(d / (3600 * 24 * 1000));
//                String mobile = userService.getById(taskUnit.getUserId()).getMobile();
                //如果今天大于任务期限时间,任务就逾期
                if (ceil > 0) {
                    taskUnit.setStatus("10");  //任务逾期
                    String s = Double.toString(ceil);
                 /*   int i = format.parse(today).compareTo(parse);
                    String s = Integer.toString(i);*/
//                更新逾期天数
                    taskUnit.setOverdueDay(s);
                    taskUnitService.update(taskUnit);
                    List<TaskUser> taskUsers = taskUserService.getByTaskUnitId(taskUnit.getSysId());
                    for (TaskUser taskUser : taskUsers) {
                        if (!"4".equals(taskUser.getStatus()) && !"2".equals(taskUser.getStatus())){
                            taskUser.setOverdueDay(s);
                            taskUserService.update(taskUser);
                        }
                    }
                    System.out.println("----------------------========-----------=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-");
                    System.out.println(taskUnit.getSysId() + taskUnit.getDeptName() + taskUnit.getTaskMainName());
                    //当前子任务就状态就改为逾期
                    TaskSub taskSub = taskSubService.getById(taskUnit.getTaskSubId());
                    if(ObjectUtils.isBlank(taskSub)){
                        continue;
                    }
                    taskSub.setType("2");
                    taskSubService.update(taskSub);
                    //逾期发送短信
                } else if (ceil == 0) {
                    if (!"4".equals(taskUnit.getType()) && !"2".equals(taskUnit.getType())) {
                        List<TaskUser> taskUsers = taskUserService.getByTaskUnitId(taskUnit.getSysId());
                        for (TaskUser taskUser : taskUsers) {
                            if (!"4".equals(taskUser.getStatus()) && !"2".equals(taskUser.getStatus())){
                                SMSEntity smsEntity = new SMSEntity();
                                smsEntity.setTaskUserId(taskUser.getSysId());
                                smsEntity.setType("2");//逾期短信
                                smsEntity.setState("0");//未发送
                                smsSerivce.save(smsEntity);
                            }
                        }
                        SMSEntity smsEntity = new SMSEntity();
                        smsEntity.setTaskUnitId(taskUnit.getSysId());
                        smsEntity.setType("2");//逾期短信
                        smsEntity.setState("0");//未发送
                        smsSerivce.save(smsEntity);
                       /* SysUserFilter userFilter = new SysUserFilter();
                        userFilter.setRoleId("02");
                        userFilter.setDeptId(taskUnit.getDeptId());
                        List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(userFilter);
                        for (SysLogin sysLogin : loginList) {
                            String mobile = userService.getById(sysLogin.getSysId()).getMobile();
                            if (StringUtils.isNotBlank(mobile)) {
                                //如果任务逾期且未办结,就发送逾期提醒短信
                                smsUtilService.departOverdue(taskUnit, mobile);
                                //给督查立项人提醒
                                smsUtilService.departOverdue(taskUnit, taskCreateUser.getMobile());
                            }
                        }*/
                    }
                } else if (ceil == -1) {

                    //发送逾期预警短信
                    if (!"4".equals(taskUnit.getType()) && !"2".equals(taskUnit.getType())) {
                        List<TaskUser> taskUsers = taskUserService.getByTaskUnitId(taskUnit.getSysId());
                        for (TaskUser taskUser : taskUsers) {
                            if (!"4".equals(taskUser.getStatus()) && !"2".equals(taskUser.getStatus())){
                                SMSEntity smsEntity = new SMSEntity();
                                smsEntity.setTaskUserId(taskUser.getSysId());
                                smsEntity.setType("1");//提前短信
                                smsEntity.setState("0");//未发送
                                smsSerivce.save(smsEntity);
                            }
                        }
                        SMSEntity smsEntity = new SMSEntity();
                        smsEntity.setTaskUnitId(taskUnit.getSysId());
                        smsEntity.setType("1");//提前短信
                        smsEntity.setState("0");//未发送
                        smsSerivce.save(smsEntity);
                       /* SysUserFilter userFilter = new SysUserFilter();
                        userFilter.setRoleId("02");
                        userFilter.setDeptId(taskUnit.getDeptId());
                        List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(userFilter);
                        for (SysLogin sysLogin : loginList) {
                            String mobile = userService.getById(sysLogin.getSysId()).getMobile();
                            if (StringUtils.isNotBlank(mobile)) {
                                //如果任务逾期且未办结,就发送逾期提醒短信
                                smsUtilService.departWarning(taskUnit, mobile);
                                //给督查立项人提醒
                                smsUtilService.departWarning(taskUnit, taskCreateUser.getMobile());

                            }
                        }*/
                    }
                }
            }
        }
        List<TaskMain> taskMainList = taskMainService.query();
        for (TaskMain taskMain : taskMainList) {
            int o = taskUnitService.getCountByStatus(taskMain.getSysId(), "10");
            taskMain.setOverdueExplain("逾期单位任务: " + o);
            taskMainService.update(taskMain);
        }

        System.out.println("！！！！定时任务结束！！！！");
    }


    /**
     * 发送短信
     */
    public void sendSMS() {
        List<SMSEntity> smsEntities = smsSerivce.queryByState("0");
        for (SMSEntity smsEntity : smsEntities) {
            if (StringUtils.isNotBlank(smsEntity.getTaskUnitId())){
                TaskUnit taskUnit = taskUnitService.getById(smsEntity.getTaskUnitId());
                SysUser taskCreateUser = userService.getById(taskUnit.getCreateUser());
                if (!"4".equals(taskUnit.getType()) && !"2".equals(taskUnit.getType())) {
                    SysUserFilter userFilter = new SysUserFilter();
                    userFilter.setRoleId("02");
                    userFilter.setDeptId(taskUnit.getDeptId());
                    List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(userFilter);
                    for (SysLogin sysLogin : loginList) {
                        SysUser user = userService.getById(sysLogin.getSysId());
                        String mobile = user.getMobile();
                        if (StringUtils.isNotBlank(mobile)) {
                            if ("1".equals(smsEntity.getType())) {
                                //提前提醒的短信
                                smsUtilService.departWarning(taskUnit, user);
                                //给督查立项人提醒
                                smsUtilService.departWarning(taskUnit, taskCreateUser);
                            } else if ("2".equals(smsEntity.getType())) {
                                //如果任务逾期且未办结,就发送逾期提醒短信
                                 smsUtilService.departOverdue(taskUnit, user);
                                //给督查立项人提醒
                                smsUtilService.departOverdue(taskUnit, taskCreateUser);
                            }
                            //发送完短信将记录改为已发送
                            smsEntity.setState("1");
                            smsSerivce.update(smsEntity);
                        }
                    }
                }
            }
            if (StringUtils.isNotBlank(smsEntity.getSpecialId())){
                SpecialTask specialTask = specialTaskService.getById(smsEntity.getSpecialId());
                String createUser = taskMainService.getById(specialTask.getTaskMainId()).getCreateUser();
                SysUser taskCreateUser = userService.getById(createUser);
                if (!"4".equals(specialTask.getType()) && !"2".equals(specialTask.getType())) {
                    SysUserFilter userFilter = new SysUserFilter();
                    userFilter.setRoleId("02");
                    if (StringUtils.isNotBlank(specialTask.getOverdueDeptId())){
                        userFilter.setDeptId(specialTask.getOverdueDeptId());
                    }else{
                        userFilter.setDeptId(specialTask.getInspectionId());
                    }
                    List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(userFilter);
                    for (SysLogin sysLogin : loginList) {
                        SysUser user = userService.getById(sysLogin.getSysId());
                        String mobile = user.getMobile();
                        if (StringUtils.isNotBlank(mobile)) {
                            if ("1".equals(smsEntity.getType())) {
                                //提前提醒的短信
                                smsUtilService.departWarning(specialTask, user);
                                //给督查立项人提醒
                                smsUtilService.departWarning(specialTask, taskCreateUser);
                            } else if ("2".equals(smsEntity.getType())) {
                                //如果任务逾期且未办结,就发送逾期提醒短信
                                 smsUtilService.departOverdue(specialTask, user);
                                //给督查立项人提醒
                                smsUtilService.departOverdue(specialTask, taskCreateUser);
                            }
                            //发送完短信将记录改为已发送
                            smsEntity.setState("1");
                            smsSerivce.update(smsEntity);
                        }
                    }
                }
            }
            if (StringUtils.isNotBlank(smsEntity.getTaskUserId())){
                TaskUser taskUser = taskUserService.getById(smsEntity.getTaskUserId());
                TaskUnit taskUnit = taskUnitService.getById(taskUser.getTaskUnitId());
                String unitUserId = taskUser.getUnitUserId();
                SysUser user = userService.getById(unitUserId);
                String mobile = user.getMobile();
                if (StringUtils.isNotBlank(mobile)) {
                    if ("1".equals(smsEntity.getType())) {
                        //提前提醒的短信
                        smsUtilService.departWarning(taskUnit, user);
                    } else if ("2".equals(smsEntity.getType())) {
                        //如果任务逾期且未办结,就发送逾期提醒短信
                         smsUtilService.departOverdue(taskUnit, user);
                        //给督查立项人提醒
//                        smsUtilService.departOverdue(taskUnit, taskCreateUser);
                    }
                    //发送完短信将记录改为已发送
                    smsEntity.setState("1");
                    smsSerivce.update(smsEntity);
                }
            }
        }
    }
}
